/**
 * Author: NingpingGou
 * Date: 2012-11-15
 * Description: 
 */
package cn.gnp.framework.core.util;

import java.security.SecureRandom;
import java.util.Random;

/**
 * @author NingpingGou
 *
 */
public class PasswordUtil {
	
	private static final Random randGenerator = new SecureRandom();

	private static final String upperChars = "QWERTYUIOPLKJHGFDSAZXCVBNM";
	private static final String lowerChars = "qwertyuioplkjhgfdsazxcvbnm";
	private static final String numericChars = "0123456789";

	private static String getSecureRandom(int size, Boolean upperCase, Boolean lowerCase, Boolean numeric) {
		StringBuffer ret = new StringBuffer(size);
		String validChars = getSeedString(upperCase, lowerCase, numeric);
		for (int i = 0; i < size; i++) {
			ret.append(validChars.charAt(Math.abs(randGenerator.nextInt())
					% validChars.length()));
		}
		return ret.toString();
	}
	
	private static String getSeedString(Boolean upperCase, Boolean lowerCase, Boolean numeric)
	{
		StringBuffer ret = new StringBuffer();
		ret.append((upperCase?upperChars:"")).append((lowerCase?lowerChars:"")).append((numeric?numericChars:""));
		return ret.toString();
	}
	
	public static String getRandomNumeric(int size)
	{
		return getSecureRandom(size, false, false, true);
	}

	public static String getRandomAlpha(int size)
	{
		return getSecureRandom(size, true, true, false);
	}
	
	public static String getRandomUpperAlpha(int size)
	{
		return getSecureRandom(size, true, false, false);
	}	
	
	public static String getRandomLowerAlpha(int size)
	{
		return getSecureRandom(size, false, true, false);
	}	
}
